package com.dyrj.question.controller;

import com.dyrj.pig.result.JsonResult;
import com.dyrj.utils.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.dyrj.question.service.IPDjjruleService;
import com.dyrj.question.bean.PDjjrule;
import org.springframework.stereotype.Controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自动发放代金券规则管理 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Controller
@Component
@RequestMapping("/question/p-djjrule")
public class PDjjruleController {

    @Resource
    private IPDjjruleService service;

    @GetMapping("/index")
//    @RequiresPermissions("question:p-djjrule:index")
    public String index(ModelMap map) {
    return "question/index_p-djjrule";
    }

    /**
    * @Title: table
    * @Description: (表格数据) 列表
    * @return PageInfo<PDjjrule>    返回类型
    */
    @GetMapping("/table")
    @ResponseBody
//    @RequiresPermissions("question:p-djjrule:index")
    public PageResultMsg table(PDjjrule record) {
        return service.table(record);
    }

    /**
    * @Title: add
    * @Description: (新增)
    * @param record
    * @return ResultMsg    返回类型
    */
    @PostMapping("/")
    @ResponseBody
//    @RequiresPermissions("question:p-djjrule:add")
    public ResultMsg add(PDjjrule record) {
        return service.add(record);
    }

    /**
    * @Title: info
    * @Description: (详情)
    * @param id
    * @return PDjjrule    返回类型
    */
    @GetMapping("/{id}")
    @ResponseBody
//    @RequiresPermissions("question:p-djjrule:info")
    public PDjjrule info(@PathVariable String id) {
        return service.info(id);
    }

    /**
    * @Title: update
    * @Description: (更新)
    * @param record
    * @return ResultMsg    返回类型
    */
    @PutMapping("/")
    @ResponseBody
//    @RequiresPermissions("question:p-djjrule:edit")
    public ResultMsg update(PDjjrule record) {
        return service.update(record);
    }

    /**
    * @Title: delete
    * @Description: (删除)
    * @param id
    * @return ResultMsg    返回类型
    */
    @DeleteMapping("/{id}")
    @ResponseBody
//    @RequiresPermissions("question:p-djjrule:delete")
    public ResultMsg delete(@PathVariable String id) {
        return service.delete(id);
    }

//    0 0 0 1 * ? 每月月初执行一次
    @PostMapping("/doTask")
    @ResponseBody
    public ResultMsg doTask(@RequestParam(required = false) Map map) {
        String ny =(String)map.get("ny");
        if(ny==null||"".equals(ny)) {
            ny = DateUtil.format(DateUtil.addDate(DateUtil.getDate(), Calendar.DATE, -2), "yyyyMM");
        }else{
            ny = DateUtil.format(DateUtil.addDate(DateUtil.parse(ny,"yyyyMM"), Calendar.DATE, -2), "yyyyMM");
        }
        ResultMsg resultMsg = service.autoSendPrize(ny);
        return resultMsg;
    }

    @Scheduled(cron="0 0 0 1 * ?")
    public ResultMsg doTask1() {
        String ny = DateUtil.format(DateUtil.addDate(DateUtil.getDate(), Calendar.DATE, -2), "yyyyMM");
        ResultMsg resultMsg = service.autoSendPrize(ny);
        return resultMsg;
    }
}
